package com.czl.module_storehouse.activity.intostore.selectorder;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.czl.module_base.activity.BaseRecyclerViewActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.SelectOrderAdapter;
import com.czl.module_storehouse.bean.PurchaseBean;
import com.czl.module_storehouse.databinding.ActivitySelectOrderBinding;
import com.czl.module_storehouse.event.PurchaseEvent;
import com.czl.module_storehouse.event.PurchaseProjectListBeanEvent;
import com.czl.module_storehouse.mvp.presenter.AddWareHousePresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class SelectOrderActivity extends BaseRecyclerViewActivity<ActivitySelectOrderBinding> implements SimpleView {

    private SelectOrderAdapter mAdapter;
    private final List<PurchaseBean> mList = new ArrayList<>();
    @InjectPresenter
    AddWareHousePresenter mAddWareHousePresenter;

    @Override
    protected ActivitySelectOrderBinding getChildBinding(LayoutInflater layoutInflater, FrameLayout containerTop) {
        return ActivitySelectOrderBinding.inflate(layoutInflater, containerTop, true);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("选择采购单");
        mAdapter = new SelectOrderAdapter(R.layout.item_select_order, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        initListener();
    }

    @Override
    protected void loadData() {
        super.loadData();
        if (mAddWareHousePresenter != null) {
            mAddWareHousePresenter.appgetRukuPurhchaseCardList();
        }
    }

    @Override
    protected void loadMore() {
        super.loadMore();
    }

    protected void initListener() {
        if (mAdapter != null) {
            mAdapter.setOnItemChildClickListener((adapter, view, position) -> clickItem(view, position));
        }
        mChildBinding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                search(s.toString());
            }
        });
    }

    private void search(String text) {
        if (TextUtils.isEmpty(text)) {
            mAdapter.setList(mList);
            return;
        }
        List<PurchaseBean> newList = new ArrayList<>();
        for (PurchaseBean bean : mList) {
            if (bean.getCardName() != null && bean.getCardName().contains(text)) {
                newList.add(bean);
            }
        }
        if (newList.isEmpty()) {
            View view = View.inflate(getContext(), R.layout.common_empty_layout, null);
            mAdapter.setList(new ArrayList<>());
            mAdapter.setEmptyView(view);
        } else {
            mAdapter.setList(newList);
        }
    }

    private void clickItem(View view, int position) {
        PurchaseBean item = mAdapter.getItem(position);
        int id = view.getId();
        if (id == R.id.iv_right) {
            selectItem(item);
        } else if (id == R.id.text_look) {
            if (item != null) {
                nextActivity(item);
            }
        }
    }

    private void nextActivity(PurchaseBean bean) {
        PurchaseProjectListBeanEvent event = new PurchaseProjectListBeanEvent();
        event.setList(bean.getCardProjectList());
        EventBus.getDefault().postSticky(event);
        startActivity(new Intent(getContext(), ViewOrderItemActivity.class));
    }

    private void selectItem(PurchaseBean bean) {
        EventBus.getDefault().postSticky(new PurchaseEvent(bean));
        finish();
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        mList.clear();
        mList.addAll(((ListBean<PurchaseBean>) t).getList());
        handleResponseData(mAdapter, mAddWareHousePresenter.getPageNo(), (ListBean<PurchaseBean>) t);
    }
}
